﻿using Script.Structs.Pawn;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Diagnostics;
using UnityEngine.Serialization;
using ZYLFramework.Manage;

public class PawnState : MonoBehaviour
{
    
    ///<summary>
    ///棋子所处阵营，阵营不同就会对打
    ///</summary>
    public string CampId;
    
    ///<summary>
    ///棋子最终的可操作性状态
    ///</summary>
    private PawnControlState _controlState = new PawnControlState();
        
    ///<summary>
    ///棋子最终的可操作性状态
    ///</summary>
    public PawnControlState ControlState{
        get
        {
            return this._controlState;
        }
    }

    private PawnProperty _prop = PawnProperty.zero;
    
    ///<summary>
    ///棋子当前的属性
    ///</summary>
    public PawnProperty Property {get{
        return _prop;
    }}
    
    ///<summary>
    ///棋子的无敌状态持续回合，如果在无敌状态中，DamageInfo处理无效化
    ///单位：回合
    ///</summary>
    public int immuneTime{
        get{
            return _immuneTime;
        }
        set{
            _immuneTime = Mathf.Max(_immuneTime, value);
        }
    }
    private int _immuneTime = 0;


    ///<summary>
    ///棋子的基础属性，也就是每个棋子“裸体”且不带任何buff的“纯粹的属性”
    ///先写死，正式的应该读表
    ///</summary>
    public PawnProperty baseProp = new PawnProperty(
        1, 1, 5, MoveType.Walk
    );
    
    ///<summary>
    ///棋子是否已经死了，这不由我这个系统判断，其他系统应该告诉我
    ///</summary>
    public bool dead = false;

    ///<summary>
    ///棋子现有的资源，比如hp之类的
    ///</summary>
    public PawnResource resource = new PawnResource(1);

    
    public GameObject Square;

    ///<summary>
    ///重新计算所有属性，并且获得一个最终属性
    ///其实这个应该走脚本函数返回，抛给脚本函数多个Property，由脚本函数运作他们的运算关系，并返回结果
    ///</summary>
    private void AttrRecheck()
    {
        //清空为0
        this._prop.Zero();
        this._prop = (this.baseProp) ;
    }
    
    ///<summary>
    ///判断这个棋子是否会被这个damageInfo所杀
    ///<param name="dInfo">要判断的damageInfo</param>
    ///<return>如果是true代表棋子可能会被这次伤害所杀</return>
    ///</summary>
    public bool CanBeKilledByDamageInfo(DamageInfo damageInfo){
        if (this.immuneTime > 0  || damageInfo.isHeal() == true) return false;
        int dValue = damageInfo.DamageValue(false);
        return dValue >= this.resource.Hp;
    }

    ///<summary>
    ///初始化棋子的属性
    ///</summary>
    public void InitBaseProp(PawnProperty pProp)
    {
        this.baseProp = pProp;
        this.AttrRecheck();
        this.resource.Hp = this._prop.Hp;
    }
    
    ///<summary>
    ///设置棋子阵营
    ///</summary>
    public void SetCamp(string campId,Color color)
    {
        CampId = campId;
        Square.GetComponent<SpriteRenderer>().color = color;
    }
    
    ///<summary>
    ///设置棋子阵营
    ///</summary>
    public void SetGuise(string path)
    {
        Debug.Log(path);
        var sprite = ResourcesManage.Instance.NewObjectAsset(path) as Sprite;
        Square.GetComponent<SpriteRenderer>().sprite = sprite;
    }
    
    ///<summary>
    ///增加棋子的血量等资源，直接改变数字的，属于最后一步操作了
    ///<param name="value">要改变的量，负数为减少</param>
    ///</summary>
    public void ModResource(PawnResource value){
        this.resource += value;
        this.resource.Hp = Mathf.Clamp(this.resource.Hp, 0, this._prop.Hp);
        if (this.resource.Hp <= 0){
            this.Kill();
        }
    }
    
    ///<summary>
    ///杀死这个棋子
    ///</summary>
    public void Kill(){
        this.dead = true;
        //播放死亡动画
        // if (unitAnim){
        //     unitAnim.Play("Dead");
        // }
        //如果不是主角，尸体就会消失
        // if (this.gameObject != SceneVariants.MainActor())
        //     this.gameObject.AddComponent<UnitRemover>().duration = 5.0f;
        SceneVariants.ClearPawn(this.transform.gameObject);
    }
    
    ///<summary>
    ///设置棋子位置
    ///</summary>
    public void SetPawnPos(Vector3 pos)
    {
        transform.position = pos;
    }
    
    ///<summary>
    ///获取棋子攻击范围
    /// 索引代表这个距离是否可以攻击
    ///</summary>
    public int[] GetAttackRange()
    {
        return new[] { 0, 1 };
    }
    
    ///<summary>
    ///获取棋子攻击力
    ///</summary>
    public int GetAttack()
    {
        return baseProp.Action ;
    }
}